package com.control.qct.businesssteward.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.control.qct.businesssteward.R;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 日期选择控件
 * Created by Administrator on 2018/1/27 0027.
 */

public class DateSelectUtils {
    private static DateSelectUtils mDateSelectUtils;
    private Context mContext;
    private String mDate;
    private PopupWindow mPop;

    public DateSelectUtils(Context context) {
        mContext = context;
    }

    public static DateSelectUtils getInstance(Context context) {
        if (mDateSelectUtils == null) {
            mDateSelectUtils = new DateSelectUtils(context);
        }
        return mDateSelectUtils;
    }

    /**
     * 设置日期选择器
     *
     * @param tv
     */
    public void setAccountDateSelectPop(Activity activity, final TextView tv) {
        View view = View.inflate(mContext, R.layout.pop_account_date_select, null);
        mPop = PopUtils.getInstance().getBottomPop(activity, view);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        TextView tvSure = view.findViewById(R.id.tv_sure);
        DatePicker dpDateSelect = view.findViewById(R.id.dp_date_select);
        //设置中文日历
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (!TextUtils.isEmpty(tv.getText().toString())) {
            String[] time = tv.getText().toString().split("-");
            year = Integer.parseInt(time[0]);
            month = Integer.parseInt(time[1]) - 1;
            day = Integer.parseInt(time[2]);
        }
        mDate = year + "-" + (month + 1) + "-" + day;
        dpDateSelect.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
                mDate = year + "-" + (month + 1) + "-" + day;
            }
        });
        //设置最大日期--当前时间
        GregorianCalendar g = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dpDateSelect.setMaxDate(g.getTimeInMillis());
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPop.dismiss();
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (!TextUtils.isEmpty(mTvMonthAccountDate1.getText().toString()) && tv.getId() !=
//                        mTvMonthAccountDate1.getId()) {
//                    if (DateUtil.stringToMillis(mDate) < DateUtil.stringToMillis(mTvMonthAccountDate1.getText()
//                            .toString())) {
//                        showShortToast("不能小于起始时间!");
//                        return;
//                    }
//                }
                tv.setText(mDate);
                mPop.dismiss();
            }
        });
        //设置PopupWindow显示位置
        mPop.showAtLocation(activity.findViewById(R.id.rv_charge_back), Gravity.BOTTOM | Gravity
                .CENTER_HORIZONTAL, 0, 0);
    }


}
